package com.ctshk.sapp.hotel.convertor;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.individual.conversion.*;
import com.ctshk.common.utils.individual.conversion.dto.*;
import com.ctshk.rpc.hotel.dto.*;
import com.ctshk.rpc.hotel.req.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Convertor {
    /**
     * 前端统一结构转成酒店产品结构，l2 to l1
     *
     * @param batchStrategyReq
     * @return
     */
    public static HotelProductStrategySaveBatchReq frontToHotelProduct(BatchStrategyReq batchStrategyReq) {
        HotelProductStrategySaveBatchReq hotelProductStrategySaveBatchReq = new HotelProductStrategySaveBatchReq();
        // 获取巴士产品ID列表
        hotelProductStrategySaveBatchReq.setHotelProductIdList(batchStrategyReq.getProductIds());
        // 获取策略列表
        List<StrategyReq> batchStrategyReqStrategyList = batchStrategyReq.getStrategyList();
        List<HotelProductStrategyReq> hotelProductStrategyReqList = new ArrayList<>();
        hotelProductStrategySaveBatchReq.setHotelProductStrategyReqList(hotelProductStrategyReqList);
        for (StrategyReq strategyReq : batchStrategyReqStrategyList) {
            HotelProductStrategyReq hotelProductStrategyReq = new HotelProductStrategyReq();
            hotelProductStrategyReqList.add(hotelProductStrategyReq);
            //策略主表
            hotelProductStrategyReq.setIncreaseStrategyName(strategyReq.getName());

            //策略时间
            List<ProductStrategyDateReq> productStrategyDateReqList = strategyReq.getTime();
            List<HotelProductStrategyDateReq> hotelProductStrategyDateReqList = EntityUtil.copyList(productStrategyDateReqList, HotelProductStrategyDateReq.class);

            hotelProductStrategyReq.setHotelProductStrategyDateReqList(hotelProductStrategyDateReqList);
            //渠道加幅规则
            ChannelSaleReq channelSaleReq = strategyReq.getChannelSaleReq();
            //渠道加幅规则-线下自营渠道
            ProductOfflineChannelReq productOfflineChannelReq = channelSaleReq.getProductOfflineChannelReq();
            hotelProductStrategyReq.setSelfSalePermissionType(productOfflineChannelReq.getPermissionType());
            if (hotelProductStrategyReq.getSelfSalePermissionType() != null) {
                if (hotelProductStrategyReq.getSelfSalePermissionType() == 2) {
                    HotelProductIncreaseStrategyRuleReq hotelProductIncreaseStrategyRule = EntityUtil.copy(productOfflineChannelReq, HotelProductIncreaseStrategyRuleReq.class);
                    hotelProductStrategyReq.setHotelProductIncreaseStrategySelfRule(hotelProductIncreaseStrategyRule);
                }
                if (hotelProductStrategyReq.getSelfSalePermissionType() == 3) {
                    List<HotelProductChannelOfflineSelfCustomReq> hotelProductChannelOfflineSelfCustomReqList = new ArrayList<>();

                    List<ProductChannelOfflineSelfCustomReq> productChannelOfflineSelfCustomReqList = productOfflineChannelReq.getProductChannelOfflineSelfCustomReqList();

                    for (ProductChannelOfflineSelfCustomReq productChannelOfflineSelfCustomReq : productChannelOfflineSelfCustomReqList) {
                        HotelProductChannelOfflineSelfCustomReq hotelProductChannelOfflineSelfCustomReq = new HotelProductChannelOfflineSelfCustomReq();
                        hotelProductChannelOfflineSelfCustomReq.setName(productChannelOfflineSelfCustomReq.getName());
                        hotelProductChannelOfflineSelfCustomReq.setCheckType(productChannelOfflineSelfCustomReq.getCheckType());
                        hotelProductChannelOfflineSelfCustomReq.setRelationId(productChannelOfflineSelfCustomReq.getRelationId());

                        HotelProductIncreaseStrategyRuleReq hotelProductIncreaseStrategyRule = EntityUtil.copy(productChannelOfflineSelfCustomReq, HotelProductIncreaseStrategyRuleReq.class);
                        hotelProductChannelOfflineSelfCustomReq.setHotelProductIncreaseStrategyRuleReq(hotelProductIncreaseStrategyRule);
                        hotelProductChannelOfflineSelfCustomReqList.add(hotelProductChannelOfflineSelfCustomReq);
                    }
                    hotelProductStrategyReq.setHotelProductChannelOfflineSelfCustomReqList(hotelProductChannelOfflineSelfCustomReqList);
                }
            }

            //渠道加幅规则-线下代理商渠道
            ProductAgentReq productAgentReq = channelSaleReq.getProductAgentReq();
            hotelProductStrategyReq.setAgentSalePermissionType(productAgentReq.getPermissionType());
            if (hotelProductStrategyReq.getAgentSalePermissionType() != null) {
                if (hotelProductStrategyReq.getAgentSalePermissionType() == 2) {
                    HotelProductIncreaseStrategyRuleReq hotelProductIncreaseStrategyRule = EntityUtil.copy(productAgentReq, HotelProductIncreaseStrategyRuleReq.class);
                    hotelProductStrategyReq.setHotelProductIncreaseStrategyAgentRule(hotelProductIncreaseStrategyRule);
                }
                if (hotelProductStrategyReq.getAgentSalePermissionType() == 3) {
                    List<HotelProductChannelOfflineAgentCustomReq> hotelProductChannelOfflineAgentCustomReqs = new ArrayList<>();

                    List<ProductChannelOfflineAgentCustomReq> productChannelOfflineAgentCustomReqList = productAgentReq.getProductChannelOfflineAgentCustomReqList();
                    for (ProductChannelOfflineAgentCustomReq productChannelOfflineAgentCustomReq : productChannelOfflineAgentCustomReqList) {
                        HotelProductChannelOfflineAgentCustomReq hotelProductChannelOfflineAgentCustomReq = new HotelProductChannelOfflineAgentCustomReq();
                        hotelProductChannelOfflineAgentCustomReq.setAgentId(productChannelOfflineAgentCustomReq.getAgentId());
                        hotelProductChannelOfflineAgentCustomReq.setAgentName(productChannelOfflineAgentCustomReq.getAgentName());

                        HotelProductIncreaseStrategyRuleReq hotelProductIncreaseStrategyRule = EntityUtil.copy(productChannelOfflineAgentCustomReq, HotelProductIncreaseStrategyRuleReq.class);
                        hotelProductChannelOfflineAgentCustomReq.setHotelProductIncreaseStrategyRuleReq(hotelProductIncreaseStrategyRule);
                        hotelProductChannelOfflineAgentCustomReqs.add(hotelProductChannelOfflineAgentCustomReq);

                    }
                    hotelProductStrategyReq.setHotelProductChannelOfflineAgentCustomReqList(hotelProductChannelOfflineAgentCustomReqs);
                }
            }

            //渠道加幅规则-线上自营渠道官方平台
            List<ProductChannelOnlineSelfPlatformReq> productChannelOnlineSelfPlatformReqList = channelSaleReq.getProductChannelOnlineSelfPlatformReqList();
            List<HotelProductChannelOnlineSelfPlatformReq> hotelProductChannelOnlineSelfPlatformReqList = new ArrayList<>();
            hotelProductStrategyReq.setHotelProductChannelOnlineSelfPlatformReqList(hotelProductChannelOnlineSelfPlatformReqList);
            HotelProductChannelOnlineSelfPlatformReq hotelProductChannelOnlineSelfPlatformReq = null;
            if (!org.springframework.util.CollectionUtils.isEmpty(productChannelOnlineSelfPlatformReqList)) {
                for (ProductChannelOnlineSelfPlatformReq productChannelOnlineSelfPlatformReq : productChannelOnlineSelfPlatformReqList) {
                    hotelProductChannelOnlineSelfPlatformReq = new HotelProductChannelOnlineSelfPlatformReq();
                    //渠道信息
                    hotelProductChannelOnlineSelfPlatformReq.setChannelId(productChannelOnlineSelfPlatformReq.getChannelId());
                    hotelProductChannelOnlineSelfPlatformReq.setChannelName(productChannelOnlineSelfPlatformReq.getChannelName());

                    HotelProductIncreaseStrategyRuleReq hotelProductIncreaseStrategyRule = EntityUtil.copy(productChannelOnlineSelfPlatformReq, HotelProductIncreaseStrategyRuleReq.class);
                    hotelProductChannelOnlineSelfPlatformReq.setHotelProductIncreaseStrategyRuleReq(hotelProductIncreaseStrategyRule);
                    hotelProductChannelOnlineSelfPlatformReqList.add(hotelProductChannelOnlineSelfPlatformReq);
                }
            }
            //渠道加幅规则-线上第三方平台
            List<ProductChannelOnlineThirdPlatformReq> productChannelOnlineThirdPlatformReqList = channelSaleReq.getProductChannelOnlineThirdPlatformReqList();
            List<HotelProductChannelOnlineThirdPlatformReq> hotelProductChannelOnlineThirdPlatformReqList = new ArrayList<>();
            hotelProductStrategyReq.setHotelProductChannelOnlineThirdPlatformReqList(hotelProductChannelOnlineThirdPlatformReqList);
            HotelProductChannelOnlineThirdPlatformReq hotelProductChannelOnlineThirdPlatformReq = null;
            if (CollectionUtils.isNotEmpty(productChannelOnlineThirdPlatformReqList)) {
                for (ProductChannelOnlineThirdPlatformReq productChannelOnlineThirdPlatformReq : productChannelOnlineThirdPlatformReqList) {
                    hotelProductChannelOnlineThirdPlatformReq = new HotelProductChannelOnlineThirdPlatformReq();
                    //渠道信息
                    hotelProductChannelOnlineThirdPlatformReq.setPlatformId(productChannelOnlineThirdPlatformReq.getPlatformId());
                    hotelProductChannelOnlineThirdPlatformReq.setPlatformName(productChannelOnlineThirdPlatformReq.getPlatformName());


                    HotelProductIncreaseStrategyRuleReq hotelProductIncreaseStrategyRule = EntityUtil.copy(productChannelOnlineThirdPlatformReq, HotelProductIncreaseStrategyRuleReq.class);
                    hotelProductChannelOnlineThirdPlatformReq.setHotelProductIncreaseStrategyRuleReq(hotelProductIncreaseStrategyRule);
                    hotelProductChannelOnlineThirdPlatformReqList.add(hotelProductChannelOnlineThirdPlatformReq);
                }
            }
        }
        return hotelProductStrategySaveBatchReq;
    }


    /**
     * 巴士产品结构转前端结构，l1 to l2
     *
     * @param hotelProductStrategyDTOList
     * @return
     */
    public static BatchStrategyDto hotelProductToFront(List<HotelProductStrategyDTO> hotelProductStrategyDTOList){

        if(CollectionUtils.isEmpty(hotelProductStrategyDTOList)){
            return null;
        }
        BatchStrategyDto batchStrategyDto = new BatchStrategyDto();
        List<Long> productIds = new ArrayList<>();
        productIds.add(hotelProductStrategyDTOList.get(0).getHotelProductId());

        List<StrategyDto> strategyList = new ArrayList<>();
        for(HotelProductStrategyDTO hotelProductStrategyDTO : hotelProductStrategyDTOList){
            StrategyDto strategyDto = new StrategyDto();
            strategyDto.setName(hotelProductStrategyDTO.getIncreaseStrategyName());
            List<ProductStrategyDateReq> productStrategyDateReqList = EntityUtil.copyList(hotelProductStrategyDTO.getHotelProductStrategyDateDTOList(), ProductStrategyDateReq.class);
            strategyDto.setTime(productStrategyDateReqList);
            ChannelSaleDto channelSaleDto = new ChannelSaleDto();

            //线下代理
            ProductAgentDto productAgentDto = new  ProductAgentDto();
            List<ProductChannelOfflineAgentCustomDto> productChannelOfflineAgentCustomDtoList = new ArrayList<>();
            if(!Objects.isNull(hotelProductStrategyDTO.getHotelIncreaseStrategyAgentRule())){
                productAgentDto = EntityUtil.copy(hotelProductStrategyDTO.getHotelIncreaseStrategyAgentRule(),ProductAgentDto.class);
            }
            List<HotelProductChannelOfflineAgentCustomDTO> hotelProductChannelOfflineAgentCustomDTOList = hotelProductStrategyDTO.getHotelProductChannelOfflineAgentCustomDTOList();
            if(!CollectionUtils.isEmpty(hotelProductChannelOfflineAgentCustomDTOList)){
                for(HotelProductChannelOfflineAgentCustomDTO hotelProductChannelOfflineAgentCustomDTO : hotelProductChannelOfflineAgentCustomDTOList){
                    ProductChannelOfflineAgentCustomDto offlineAgentCustomDto = EntityUtil.copy(hotelProductChannelOfflineAgentCustomDTO, ProductChannelOfflineAgentCustomDto.class);
//                    offlineAgentCustomDto.setAgentId(hotelProductChannelOfflineAgentCustomDTO.getAgentId());
//                    offlineAgentCustomDto.setAgentName(hotelProductChannelOfflineAgentCustomDTO.getAgentName());
                    HotelProductIncreaseStrategyRuleDTO hotelProductIncreaseStrategyRuleDTO = hotelProductChannelOfflineAgentCustomDTO.getHotelProductIncreaseStrategyRuleDTO();
//                    offlineAgentCustomDto = EntityUtil.copy(hotelProductIncreaseStrategyRuleDTO,ProductChannelOfflineAgentCustomDto.class);
                    if (hotelProductIncreaseStrategyRuleDTO != null) {
                        offlineAgentCustomDto.setPriceType(hotelProductIncreaseStrategyRuleDTO.getPriceType());
                        offlineAgentCustomDto.setPrice(hotelProductIncreaseStrategyRuleDTO.getPrice());
                        offlineAgentCustomDto.setSaleableType(hotelProductIncreaseStrategyRuleDTO.getSaleableType());
                        offlineAgentCustomDto.setSaleableNumber(hotelProductIncreaseStrategyRuleDTO.getSaleableNumber());
                    }
                    productChannelOfflineAgentCustomDtoList.add(offlineAgentCustomDto);
                }
                productAgentDto.setProductChannelOfflineAgentCustomReqList(productChannelOfflineAgentCustomDtoList);
            }
            productAgentDto.setPermissionType(hotelProductStrategyDTO.getAgentSalePermissionType());
            channelSaleDto.setProductAgentReq(productAgentDto);

            //线下自营
            ProductOfflineChannelDto productOfflineChannelDto = new ProductOfflineChannelDto();
            List<ProductChannelOfflineSelfCustomDto> productChannelOfflineSelfCustomDtoList = new ArrayList<>();
            if(!Objects.isNull(hotelProductStrategyDTO.getHotelIncreaseStrategySelfRule())){
                productOfflineChannelDto = EntityUtil.copy(hotelProductStrategyDTO.getHotelIncreaseStrategySelfRule(),ProductOfflineChannelDto.class);
            }
            List<HotelProductChannelOfflineSelfCustomDTO> hotelProductChannelOfflineSelfCustomDTOList = hotelProductStrategyDTO.getHotelProductChannelOfflineSelfCustomDTOList();
            if(!CollectionUtils.isEmpty(hotelProductChannelOfflineSelfCustomDTOList)){
                for(HotelProductChannelOfflineSelfCustomDTO hotelProductChannelOfflineSelfCustomDTO : hotelProductChannelOfflineSelfCustomDTOList){
                    ProductChannelOfflineSelfCustomDto offlineSelfCustomDto = EntityUtil.copy(hotelProductChannelOfflineSelfCustomDTO, ProductChannelOfflineSelfCustomDto.class);
//                    offlineSelfCustomDto.setRelationId(hotelProductChannelOfflineSelfCustomDTO.getRelationId());
//                    offlineSelfCustomDto.setName(hotelProductChannelOfflineSelfCustomDTO.getName());
                    HotelProductIncreaseStrategyRuleDTO hotelProductIncreaseStrategyRuleDTO = hotelProductChannelOfflineSelfCustomDTO.getHotelProductIncreaseStrategyRuleDTO();
//                    offlineSelfCustomDto = EntityUtil.copy(hotelProductIncreaseStrategyRuleDTO,ProductChannelOfflineSelfCustomDto.class);
                    if (hotelProductIncreaseStrategyRuleDTO != null) {
                        offlineSelfCustomDto.setPriceType(hotelProductIncreaseStrategyRuleDTO.getPriceType());
                        offlineSelfCustomDto.setPrice(hotelProductIncreaseStrategyRuleDTO.getPrice());
                        offlineSelfCustomDto.setSaleableType(hotelProductIncreaseStrategyRuleDTO.getSaleableType());
                        offlineSelfCustomDto.setSaleableNumber(hotelProductIncreaseStrategyRuleDTO.getSaleableNumber());
                    }
                    productChannelOfflineSelfCustomDtoList.add(offlineSelfCustomDto);
                }
                productOfflineChannelDto.setProductChannelOfflineSelfCustomReqList(productChannelOfflineSelfCustomDtoList);
            }
            productOfflineChannelDto.setPermissionType(hotelProductStrategyDTO.getSelfSalePermissionType());
            channelSaleDto.setProductOfflineChannelReq(productOfflineChannelDto);

            //线上自营
            List<ProductChannelOnlineSelfPlatformDto> productChannelOnlineSelfPlatformDtoList = new ArrayList<>();
            List<HotelProductChannelOnlineSelfPlatformDTO> hotelProductChannelOnlineSelfPlatformDTOList = hotelProductStrategyDTO.getHotelProductChannelOnlineSelfPlatformDTOList();
            if(!CollectionUtils.isEmpty(hotelProductChannelOnlineSelfPlatformDTOList)){
                for(HotelProductChannelOnlineSelfPlatformDTO hotelProductChannelOnlineSelfPlatformDTO : hotelProductChannelOnlineSelfPlatformDTOList){
                    ProductChannelOnlineSelfPlatformDto productChannelOnlineSelfPlatformDto = EntityUtil.copy(hotelProductChannelOnlineSelfPlatformDTO, ProductChannelOnlineSelfPlatformDto.class);
//                    productChannelOnlineSelfPlatformDto.setChannelId(hotelProductChannelOnlineSelfPlatformDTO.getChannelId().longValue());
//                    productChannelOnlineSelfPlatformDto.setChannelName(hotelProductChannelOnlineSelfPlatformDTO.getChannelName());
                    HotelProductIncreaseStrategyRuleDTO hotelProductIncreaseStrategyRuleDTO = hotelProductChannelOnlineSelfPlatformDTO.getHotelProductIncreaseStrategyRuleDTO();
//                    productChannelOnlineSelfPlatformDto = EntityUtil.copy(hotelProductIncreaseStrategyRuleDTO,ProductChannelOnlineSelfPlatformDto.class);
                    if (hotelProductIncreaseStrategyRuleDTO != null) {
                        productChannelOnlineSelfPlatformDto.setPriceType(hotelProductIncreaseStrategyRuleDTO.getPriceType());
                        productChannelOnlineSelfPlatformDto.setPrice(hotelProductIncreaseStrategyRuleDTO.getPrice());
                        productChannelOnlineSelfPlatformDto.setSaleableType(hotelProductIncreaseStrategyRuleDTO.getSaleableType());
                        productChannelOnlineSelfPlatformDto.setSaleableNumber(hotelProductIncreaseStrategyRuleDTO.getSaleableNumber());
                    }
                    productChannelOnlineSelfPlatformDtoList.add(productChannelOnlineSelfPlatformDto);
                }
                channelSaleDto.setProductChannelOnlineSelfPlatformReqList(productChannelOnlineSelfPlatformDtoList);
            }

            //线上第三方
            List<ProductChannelOnlineThirdPlatformDto> productChannelOnlineThirdPlatformDtoList = new ArrayList<>();
            List<HotelProductChannelOnlineThirdPlatformDTO> hotelProductChannelOnlineThirdPlatformDTOList = hotelProductStrategyDTO.getHotelProductChannelOnlineThirdPlatformDTOList();
            if(!CollectionUtils.isEmpty(hotelProductChannelOnlineThirdPlatformDTOList)){
                for(HotelProductChannelOnlineThirdPlatformDTO hotelProductChannelOnlineThirdPlatformDTO : hotelProductChannelOnlineThirdPlatformDTOList){
                    ProductChannelOnlineThirdPlatformDto productChannelOnlineThirdPlatformDto = EntityUtil.copy(hotelProductChannelOnlineThirdPlatformDTO, ProductChannelOnlineThirdPlatformDto.class);
//                    productChannelOnlineThirdPlatformDto.setPlatformId(hotelProductChannelOnlineThirdPlatformDTO.getPlatformId());
//                    productChannelOnlineThirdPlatformDto.setPlatformName(hotelProductChannelOnlineThirdPlatformDTO.getPlatformName());
                    HotelProductIncreaseStrategyRuleDTO hotelProductIncreaseStrategyRuleDTO = hotelProductChannelOnlineThirdPlatformDTO.getHotelProductIncreaseStrategyRuleDTO();
//                    productChannelOnlineThirdPlatformDto = EntityUtil.copy(hotelProductIncreaseStrategyRuleDTO,ProductChannelOnlineThirdPlatformDto.class);
                    if (hotelProductIncreaseStrategyRuleDTO != null) {
                        productChannelOnlineThirdPlatformDto.setPriceType(hotelProductIncreaseStrategyRuleDTO.getPriceType());
                        productChannelOnlineThirdPlatformDto.setPrice(hotelProductIncreaseStrategyRuleDTO.getPrice());
                        productChannelOnlineThirdPlatformDto.setSaleableType(hotelProductIncreaseStrategyRuleDTO.getSaleableType());
                        productChannelOnlineThirdPlatformDto.setSaleableNumber(hotelProductIncreaseStrategyRuleDTO.getSaleableNumber());
                    }
                    productChannelOnlineThirdPlatformDtoList.add(productChannelOnlineThirdPlatformDto);
                }
                channelSaleDto.setProductChannelOnlineThirdPlatformReqList(productChannelOnlineThirdPlatformDtoList);
            }
            strategyDto.setChannelSaleReq(channelSaleDto);
            strategyList.add(strategyDto);

        }
        batchStrategyDto.setProductIds(productIds);
        batchStrategyDto.setStrategyList(strategyList);
        return batchStrategyDto;

    }

}
